Ext.define('BenBen.modules.system.organization.UserSearchWindow', {
	extend : 'Ext.window.Window',
	width : 500,
    height : 500,
    multiSelect : false,
    fromHandle : false,
	initComponent : function() {
        var panel = this;
        
        var name = Ext.create('Ext.form.TextField',{
            fieldLabel: '用户',
            width : 120,
            labelWidth :  60,
            labelAlign : 'right',
            name  :'name'
        });
        var searchButton = Ext.create('Ext.Button',{
			text : '查询',
			width : 60,
			iconCls : 'gridsearch',
			handler : function() {
				store.load({
					params : {
						keyword : name.getValue()
					}
				});
			}
		});
        var form = Ext.create('Ext.form.Panel', {
        	border : false,
            bodyStyle : 'padding:5px',
            items : [{
            	autoHeight : true,
                layout : 'column',
                bodyStyle : 'padding:5px',
                items : [{
                    columnWidth : .33,
                    layout : 'form',
                    border : false,
                    items : [name]
                }, {
                    columnWidth : .33,
                    layout : 'form',
                    border : false,
                    items : [searchButton]
                }]
            }]
        });
        
        Ext.define('dataModel', {
			extend : 'Ext.data.Model',
			fields : ['userId','departmentLongName', 'username', 'departmentName','phone','email','deptId'],
			idProperty : 'userId'
		});
        var store = new Ext.create('Ext.data.Store', {
			model : 'dataModel',
			remoteSort : true,
			proxy: {
		        type: 'rest',
		        actionMethods: {
	                create : 'POST',
	                read   : 'GET',
	                update : 'POST',
	                destroy: 'POST'
	            },
                url : 'system/user',
		        reader: {
		            type: 'json',
		            root: 'data',
		            totalProperty: 'totalCount'
		        }
		     }
		});
        if(panel.departmentId){
        	store.load({
				params : {
					departmentId  : panel.departmentId
				}
			});
        }
        var gridPanel = Ext.create('Ext.grid.Panel', {
			selType: 'checkboxmodel',
			multiSelect : panel.multiSelect,
            autoScroll : true,
			columns : [{
                xtype: 'rownumberer',
                width: 30,
                sortable: false,
                locked: true
            },{
                dataIndex: 'username',
                text: '用户'
            },{
                dataIndex: 'departmentLongName',
                text: '部门'
            },{
                dataIndex: 'phone',
                text: '联系电话'
            },{
            	dataIndex: 'email',
            	text: 'Email'
            }],
			height: 400,
			store : store,
			loadMask : true,
			viewConfig : {
				stripeRows : true,
				enableTextSelection : true
			}
		});
        
        var bbar = new Ext.Toolbar({
            items : [{
                xtype: 'tbfill'
            },{
                text : '确定',
                iconCls : 'formsave',
                handler : function() {
                	if(gridPanel.getSelectionModel() != null){
                		
                		if(panel.targetForm){
                			
                			var record = gridPanel.getSelectionModel().getSelection()[0];
                			
                			if(panel.fromHandle){
                				panel.targetForm.getForm().findField('trueName').setValue(record.get('username'));
                				panel.targetForm.getForm().findField('username').setValue(record.get('userId'));
                			}else{
                				panel.deviceStore.load({
                    				params : {
                    					name : record.get('userId')
                    				}
                    			});
                    			panel.targetForm.getForm().findField('trueName').setValue(record.get('username'));
                    			panel.targetForm.getForm().findField('departmentName').setValue(record.get('departmentLongName'));
                    			panel.targetForm.getForm().findField('phone').setValue(record.get('phone'));
                    			panel.targetForm.getForm().findField('email').setValue(record.get('email'));
                    			panel.targetForm.getForm().findField('departmentId').setValue(record.get('deptId'));
                    			panel.targetForm.getForm().findField('username').setValue(record.get('userId'));
                    			panel.targetForm.getForm().findField('deviceConfigure').setValue('');
                			}
                			
                			panel.hide();
                		}else if(panel.distributeStore){
                			var records = gridPanel.getSelectionModel().getSelection();
                			for (var i = 0, record; record = records[i]; i++) {
                				panel.distributeStore.add({
                					'username' : record.get('userId'),
                					'trueName' : record.get('username')
                				});
        					}
                			panel.hide();
                		}
                	}
                }
            }, {
                text : '取消',
                iconCls : 'formcancle',
                handler : function() {
                    panel.hide();
                }
            },{
                xtype: 'tbfill'
            }]
        });


		Ext.apply(this, {
			maximizable : true,
			closeAction : 'hide',
			layout : 'anchor',
            title : '用户查询',
            border : false,
            bbar : bbar,
			items : [form,gridPanel]
		});
		this.callParent(arguments);
	}
});